package net.yuyoo.uusession.support;

import java.math.BigInteger;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * JSON 工具类
 * @author zhandl (yuyoo zhandl@hainan.net)
 * @teme 2010-5-21 上午09:50:58
 */
public final class JsonUtil {
 	
	private static JsonFactory jsonFactory = new JsonFactory();
	
	private static ObjectMapper mapper = null;
	
	static {
		jsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		jsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		jsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		mapper = new ObjectMapper(jsonFactory);
	}
	
	/**
	 * 获取jackson json lib的ObjectMapper对象
	 * @return -- ObjectMapper对象
	 */
	public static ObjectMapper getMapper() {
		return mapper;
	}

	/**
	 * 获取jackson json lib的JsonFactory对象
	 * @return --  JsonFactory对象
	 */
	public static JsonFactory getJsonFactory() {
		return jsonFactory;
	}

	/**
	 * 将json转成java bean
	 * @param <T> -- 多态类型
	 * @param json -- json字符串
	 * @param clazz -- java bean类型(Class)
	 * @return -- java bean对象
	 */
	public static <T> T toBean(String json, Class<T> clazz) {
		
		T rtv = null;
		try {
			rtv = mapper.readValue(json, clazz); 
		} catch (Exception ex) {  
			throw new IllegalArgumentException("json字符串转成java bean异常", ex);
		}
		return rtv;		
	}
	
	/**
	 * 将java bean转成json
	 * @param bean -- java bean
	 * @return -- json 字符串
	 */
	public static String toJson(Object bean) {
		
		String rtv = null;
		try {
			rtv = mapper.writeValueAsString(bean);
		} catch (Exception ex) {  
			throw new IllegalArgumentException("java bean转成json字符串异常", ex);
		}
		return rtv;		
	}
	
	/**
	 * 将value对象转成long
	 * @param value -- 数字类型的对象
	 * @return -- long值
	 */
	public static long toLong(Object value) {
		
		long val = 0;
		if (Long.class.isInstance(value)) {
			Long l = (Long)value;
			val = l.longValue();
		} else if (Integer.class.isInstance(value)) {
			Integer i = (Integer)value;
			val = i.longValue();
		} else if (BigInteger.class.isInstance(value)) {
			BigInteger bi = (BigInteger )value;
			val = bi.longValue();
		} else if (Double.class.isInstance(value)) {
			Double d = (Double)value;
			val = d.longValue();
		} else if (Short.class.isInstance(value)) {
			Short s = (Short)value;
			val = s.shortValue();
		}
		return val;
	}

	
	/**
	 * 将value对象转成int
	 * @param value -- 数字类型的对象
	 * @return -- int值
	 */
	public static int toInt(Object value) {

		int val = 0;
		if (Long.class.isInstance(value)) {
			Long l = (Long)value;
			val = l.intValue();
		} else if (Integer.class.isInstance(value)) {
			Integer i = (Integer)value;
			val = i.intValue();
		} else if (BigInteger.class.isInstance(value)) {
			BigInteger bi = (BigInteger )value;
			val = bi.intValue();
		} else if (Double.class.isInstance(value)) {
			Double d = (Double)value;
			val = d.intValue();
		} else if (Short.class.isInstance(value)) {
			Short s = (Short)value;
			val = s.shortValue();
		}
		return val;
	}

	
	/**
	 * 将value对象转成float
	 * @param value -- 数字类型的对象
	 * @return -- float值
	 */
	public static float toFloat(Object value) {

		float val = 0;
		if (Long.class.isInstance(value)) {
			Long l = (Long)value;
			val = l.floatValue();
		} else if (Integer.class.isInstance(value)) {
			Integer i = (Integer)value;
			val = i.floatValue();
		} else if (BigInteger.class.isInstance(value)) {
			BigInteger bi = (BigInteger )value;
			val = bi.floatValue();
		} else if (Double.class.isInstance(value)) {
			Double d = (Double)value;
			val = d.floatValue();
		} else if (Short.class.isInstance(value)) {
			Short s = (Short)value;
			val = s.shortValue();
		}
		return val;
	} 
	
	/**
	 * 将value对象转成short
	 * @param value -- 数字类型的对象
	 * @return -- short值
	 */
	public static short toShort(Object value) {

		short val = 0;
		if (Long.class.isInstance(value)) {
			Long l = (Long)value;
			val = l.shortValue();
		} else if (Integer.class.isInstance(value)) {
			Integer i = (Integer)value;
			val = i.shortValue();
		} else if (BigInteger.class.isInstance(value)) {
			BigInteger bi = (BigInteger )value;
			val = bi.shortValue();
		} else if (Double.class.isInstance(value)) {
			Double d = (Double)value;
			val = d.shortValue();
		} else if (Short.class.isInstance(value)) {
			Short s = (Short)value;
			val = s.shortValue();
		}
		return val;
	}
	
	/**
	 * 将value对象转成double
	 * @param value -- 数字类型的对象
	 * @return -- double值
	 */
	public static double toDouble(Object value) {

		double val = 0;
		if (Long.class.isInstance(value)) {
			Long l = (Long)value;
			val = l.doubleValue();
		} else if (Integer.class.isInstance(value)) {
			Integer i = (Integer)value;
			val = i.doubleValue();
		} else if (BigInteger.class.isInstance(value)) {
			BigInteger bi = (BigInteger )value;
			val = bi.doubleValue();
		} else if (Double.class.isInstance(value)) {
			Double d = (Double)value;
			val = d.doubleValue();
		} else if (Short.class.isInstance(value)) {
			Short s = (Short)value;
			val = s.shortValue();
		}
		return val;
	}

	/**
	 * 将value对象转成boolean
	 * @param value -- Boolean类型的对象
	 * @return -- boolean值
	 */
	public static boolean toBoolean(Object value) {
		
		if (null == value) {
			return false;
		} else if (Boolean.class.isInstance(value)) {
			Boolean b = (Boolean)value;
			return b.booleanValue();
		} else {
			String s = value.toString();
			return !"false".equals(s) && !"0".equals(s);
		}
	}
 
	/**
	 * 防止非法实例化
	 */
	private JsonUtil() {}
}
